<?php
namespace app\madmin\controller;
use app\madmin\model\SystemGroupModel;
use app\madmin\model\SystemRuleModel;

/**
 * 权限控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class SystemGroup extends PublicBase{

	# 权限列表方法
    public function showList(){
        $SystemGroup = new SystemGroupModel;

        $Where = $this->getFormWhere();

        $Admin = $this->checkLogin();

        if($Admin['AdminID'] != 1){
            $Where['group_id'] = ['NEQ', 1];
        }

        $Lists = $SystemGroup->mList($Where, '*', config('CNF_LISTPAGE'));

        return $this->fetch('SystemGroup/showList', [
            'Lists' => $Lists
        ]);
    }

    # 权限新增方法
    public function showAdd(){
        $SystemGroup = new SystemGroupModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $SystemGroup->mAdd($Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            return $this->fetch('SystemGroup/showAdd', [
                'Info' => Null,
                'Rule' => $this->showMenu()
            ]);
        }
    }

    # 权限更新方法
    public function showEdit(){
        $SystemGroup = new SystemGroupModel;

        $ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $SystemGroup->mEdit(['group_id'=>$ID], $Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $SystemGroup->mInfo(['group_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }

            return $this->fetch('SystemGroup/showAdd', [
                'Info' => $Info,
                'Rule' => $this->showMenu()
            ]);
        }
    }

    # 权限删除方法
    public function showDel(){
    	if(request()->isPost()){
            $SystemGroup = new SystemGroupModel;

            $ID = input('post.id');

            $Back = $SystemGroup->mDel(['group_id'=>$ID]);

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

    # 获取菜单方法
    private function showMenu(){
        $SystemRule = new SystemRuleModel;

        $Where = [];
        $Where['rule_state'] = 1;

        $Admin = $this->checkLogin();

        if($Admin['AdminID'] != 1){
            $Where['rule_hide'] = 1;
        }
        
        $Lists = $SystemRule->mAll($Where, 'rule_id,rule_pid,rule_title', 'rule_sort DESC,rule_id ASC');

        return $SystemRule::mMenu($Lists);
    }

}